package com.deep.spring.v5;


import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;


@Slf4j
@Validated
@RestController
public class PhoneTest {

    @PostMapping(value = "/testPhone", produces = "text/plain;charset=UTF-8")
    @ResponseBody
    public String ValidationResultTest(
            @Valid @RequestBody ValidationParam validationParam, BindingResult results) {
        if (results.hasErrors()) {
            return "请求参数异常,错误信息为:" + results.getFieldError().getDefaultMessage();
        }
        log.info("validationParam  {}", validationParam);
        return "success";
    }

    public static void main(String[] args) {
        ValidationParam validationParam = new ValidationParam();
        validationParam.setPhone("adsf");
        validationParam.setUsername("adsf");
        validationParam.setIdCard("adsf");

        System.out.println(validationParam);
    }
}
